﻿Namespace Diagnostics
    ''' <summary>
    ''' Supports Output Debugging and Diagnostics Reports the Arps way
    ''' </summary>
    ''' <remarks></remarks>
    Public NotInheritable Class LogFacility
        ' | Arps Logging Facility                                               |
        ' | Diagnostics and Log Handler                                         |
        ' | Supports Output Debugging and Diagnostics Reports                   |
        ' | By KDERazorback                                                     |
        ' | Project Started: 24/04/2011                                         |
        ' +=====================================================================+

        ' RAW Facility
        Private _Facility As String

        ' New Line Feed
        Private _LineEnd As String = Chr(13) & Chr(10)

        'Private _ForceTriggering As Boolean
        ''' <summary>
        ''' Specified if Event Triggering support is always enabled or controlled by the "triggerEvents" argument inside AddLog method
        ''' </summary>
        ''' <value>Boolean, When true, AddLog will always trigger events</value>
        ''' <returns>A value indicating if Event Triggering is always enabled</returns>
        ''' <remarks></remarks>
        Public Property ForceEventTriggering As Boolean

        ''' <summary>
        ''' Raised when the Output Facility has changed as a result of AddLog
        ''' </summary>
        ''' <param name="text">The new line that has been added as a result of AddLog method</param>
        ''' <remarks></remarks>
        Public Event FacilityChanged(ByVal text As String)


        ''' <summary>
        ''' Adds text to the attached Output Facility
        ''' </summary>
        ''' <param name="text">Text to append to the Facility</param>
        ''' <param name="includeTimestamp">When true, a TimeStamp will be inserted before the text</param>
        ''' <param name="triggerEvents">When true, an Event will be triggered when the operation ends</param>
        ''' <remarks>To support Realtime Output monitoring, handle the event "FacilityChanged" and set "ForceEventTriggering" to true</remarks>
        Public Sub AddLog(ByVal text As String, ByVal includeTimestamp As Boolean, ByVal triggerEvents As Boolean)
            Dim buffer As String = ""

            If includeTimestamp = True Then
                ' TimeStamp has this Syntax
                ' [HH:MM]
                ' Where HH is the two digits Hour and MM is the two digits Minutes, if hours or minutes are less than 10, a leading "0" will be added, resulting in "0X"

                Dim stamp As Date = Now
                Dim hours As String
                Dim minutes As String
                hours = stamp.Hour
                minutes = stamp.Minute
                stamp = Nothing
                If hours.Length = 1 Then
                    hours = "0" & hours
                ElseIf hours.Length = 0 Then
                    hours = "00"
                End If
                If minutes.Length = 1 Then
                    minutes = "0" & minutes
                ElseIf minutes.Length = 0 Then
                    minutes = "00"
                End If

                buffer = "[" & hours & ":" & minutes & "] "
            End If

            buffer &= text

            If _Facility = "" Then
                _Facility = buffer
            Else
                _Facility &= _LineEnd & buffer
            End If

            If (ForceEventTriggering = True) Or (triggerEvents = True) Then
                RaiseEvent FacilityChanged(buffer)
            End If
        End Sub
        ''' <summary>
        ''' Adds text to the attached Output Facility
        ''' </summary>
        ''' <param name="text">Text to append to the Facility</param>
        ''' <remarks>This is an Overload of AddLog where "includeTimestamp" and "triggerEvents" are defaulted to False</remarks>
        Public Sub AddLog(ByVal text As String)
            AddLog(text, False, False)
        End Sub
        ''' <summary>
        ''' Adds text to the attached Output Facility
        ''' </summary>
        ''' <param name="text">Text to append to the Facility</param>
        ''' <param name="includeTimestamp">When true, a TimeStamp will be inserted before the text</param>
        ''' <remarks>This is an Overload of AddLog where "triggerEvents" is defaulted to False</remarks>
        Public Sub AddLog(ByVal text As String, ByVal includeTimestamp As Boolean)
            AddLog(text, includeTimestamp, False)
        End Sub

        ''' <summary>
        ''' Clears the Entire logging Facility
        ''' </summary>
        ''' <remarks>This is only usefull when the log is exported to some media, and is not needed anymore on this Buffer. Clearing this Facility sometimes also helps for the overall API performance</remarks>
        Public Sub ClearFacility()
            _Facility = ""
            AddLog("Facility Cleared.", True)
        End Sub

        Public Function GetFacility() As String
            Return _Facility
        End Function
    End Class
End Namespace

